package com.zito.health.service.impl;

import com.zito.health.common.service.impl.BaseServiceImpl;
import com.zito.health.entity.*;
import com.zito.health.mapper.*;
import com.zito.health.service.ItemCooperationService;
import com.zito.health.service.ItemService;
import com.zito.health.vo.ItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2019/11/11.
 */
@Service
public class ItemServiceImpl extends BaseServiceImpl<Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemScheduleMapper itemScheduleMapper;
    @Autowired
    private ItemInchargeMapper itemInchargeMapper;
    @Autowired
    private ItemResearcherMapper itemResearcherMapper;
    @Autowired
    private ItemCooperationMapper itemCooperationMapper;
    @Autowired
    private ItemTextMapper itemTextMapper;
    @Autowired
    private ItemFeasibilityMapper itemFeasibilityMapper;
    @Autowired
    private ItemExpertDeclareMapper itemExpertDeclareMapper;
    @Autowired
    private ItemExpertConclusionMapper itemExpertConclusionMapper;

    @Override
    public List<ItemVo> queryList(ItemVo itemVo) {
        return itemMapper.queryList(itemVo);
    }

    @Override
    public Item info(Long id) {
        return itemMapper.info(id);
    }

    @Override
    public Integer submitConclusion(Item item) {
        Integer flag=0;
        ItemSchedule is=new ItemSchedule();
        is.setItemId(item.getId());
        is.setScheduleValue(100);
        List<ItemSchedule> list=itemScheduleMapper.select(is);
        if(list != null && list.size()>0){
            flag=1;
        }

        return flag;
    }

    @Override
    public String canSubmit(Item item) {

        Integer id=item.getId();

        //负责人情况
        ItemIncharge itemIncharge = new ItemIncharge();
        itemIncharge.setItemId(id);
        itemIncharge = itemInchargeMapper.selectOne(itemIncharge);
        if(itemIncharge==null){
            return "您未填写负责人情况";
        }
        //主要研究人员
        ItemResearcher itemResearcher=new ItemResearcher();
        itemResearcher.setItemId(id);
        List<ItemResearcher>  itemResearchers=itemResearcherMapper.select(itemResearcher);
        if(itemResearchers==null||itemResearchers.size()==0){
            return "您未填写主要研究人员";
        }
        //申请承担单位（不用查，保存基本信息的时候就已经保存申请承担单位了）

        //协作单位（不需要必填）
//        ItemCooperation itemCooperation=new ItemCooperation();
//        itemCooperation.setItemId(id);
//        List<ItemCooperation>  itemCooperations=itemCooperationMapper.select(itemCooperation);
//        if(itemCooperations==null||itemCooperations.size()==0){
//            return "您未填写协作单位";
//        }
        //内容指标、创新点、进度和分工
        ItemText itemText = new ItemText();
        itemText.setItemId(id);
        itemText = itemTextMapper.selectOne(itemText);
        if(itemText==null){
            return "您未填写内容指标、创新点、进度和分工";
        }

        //可行性研究报告
        ItemFeasibility itemFeasibility = new ItemFeasibility();
        itemFeasibility.setItemId(id);
        itemFeasibility = itemFeasibilityMapper.selectOne(itemFeasibility);
        if(itemFeasibility==null){
            return "您未填写可行性研究报告";
        }

        return "ok";
    }

    @Override
    @Transactional
    public String lxExpertSub(ItemExpertDeclare  itemExpertDeclare) {
        // 查询当前专家有没有提交评审
        List<ItemExpertDeclare> list=itemExpertDeclareMapper.select(itemExpertDeclare);
        // 查询此科研项目下除了当前专家还有没有其他专家未提交评审的
        Integer querySize = itemExpertDeclareMapper.queryLisyByItemIdAndStates(itemExpertDeclare);
        // 先判断有没有评审
        if((list != null) && (list.size() > 0) && (list.get(0).getStandardId() != 0)){
            // 没有专家了就修改科研项目状态为 3 = 专家审核完成
            if((querySize != null) && (querySize == 0)){
                Item item = new Item();
                item.setId(itemExpertDeclare.getItemId());
                item.setStates(3);
                itemMapper.updateByPrimaryKeySelective(item);
            }
            // 如果还有专家没有评审完，先修改当前专家的评审状态为评审完毕
            itemExpertDeclareMapper.updateByItemIdAndExpertId(itemExpertDeclare);
        }else{
            return "请先评审！";
        }
        return "ok";
    }

    /**
     * 专家结题提交评审
     * @param itemExpertConclusion
     * sun updataTime 2020年6月20日13:37:02
     * @return
     */
    @Override
    @Transactional
    public String jtExpertSub(ItemExpertConclusion  itemExpertConclusion) {
        // 查询当前专家是否评审
        itemExpertConclusion = itemExpertConclusionMapper.selectOne(itemExpertConclusion);
        // 查询当前结题除了自己还有没有未提交结题评审的专家
        Integer querySize = itemExpertConclusionMapper.querySize(itemExpertConclusion.getItemId(),itemExpertConclusion.getExpertId());
        if(itemExpertConclusion!=null){
            // 没有专家需要提交了就把状态改为  types = 2 states = 3 代表 结题专家已审核完成
            if((querySize != null) && (querySize == 0)){
                Item item = new Item();
                item.setId(itemExpertConclusion.getItemId());
                item.setStates(3);
                itemMapper.updateByPrimaryKeySelective(item);
            }
            // 如还有为提交评审的专家就先修改当期专家评审
            itemExpertConclusionMapper.updateStatesByItemId(itemExpertConclusion.getItemId(),itemExpertConclusion.getExpertId());
        }else{
            return "请先评审！";
        }
        return "ok";
    }


    /**
     * 退回科研
     * @param item
     * @auth sun
     */
    @Override
    @Transactional
    public void sendItem(Item item) {
        // 1 = 立项 2 = 结题   states = 2;3;4;5 分配过专家
        if ((item.getTypes() == 1) ){
            if((item.getStates() == 2) || (item.getStates() == 3)){
                // 分配的专家删除
                itemExpertDeclareMapper.deletByItemid(item.getId());
            }
        }else if((item.getTypes() == 2)){
            if((item.getStates() == 2) || (item.getStates() == 3)){
                itemExpertConclusionMapper.deletByItemid(item.getId());
            }
        }
        itemMapper.sendItem(item);
    }
}
